layui.use(['form', 'table', 'laydate', 'layarea', 'tableSelect'], function () {
    let $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layarea = layui.layarea;


    /*城市级联*/
    layarea.render({
        elem: '#area-picker',
        change: function (res) {
            //选择结果
            submitInfo();
        }
    });
    /*订单列表*/
    table.render(
        {
        elem: '#currentTableId',
        url: '/admin/order/index?action=ajax',
        method: 'post',
        parseData: function (res) { //res 即为原始返回的数据
            $('#ToSendPrice').html(res.tosendprice);
            $('#ToSendCate').html(res.tosendcate);
            $('#ToSendNum').html(res.tosendnum);
            // return {
            //     "code": res.code, //解析接口状态
            //     "msg": res.msg, //解析提示文本
            //     "count": res.data.total, //解析数据长度
            //     "data": res.data, //解析数据列表
            // };
        },
        toolbar: '#toolbarDemo',
        defaultToolbar: [ 'exports', 'print'],
        cols: [[
            // {field: 'mod_id', title: 'mod_id' },
            {field: 'bizpro_name', title: '商品名称' ,width:500},
            {field: 'bizpro_number', title: '商品编号' },
            {field: 'bizpro_spec_value', title: '商品规格' },
            {field: 'buy_price', title: '商品单价' },
            {field: 'supplier_name', title: '供应商',  },
            {field: 'bizpro_num', title: '数量',width:100},
            {field: 'orderdetail_status', title: '状态',templet:"#Tplstatus"},
            {title: '操作', toolbar: '#currentTableBar'}
        ]],
        limits: [10, 15, 20, 25, 30, 50, 100],
        limit: 10,
        page: true,
        skin: 'line,row',
        done: function (res, curr, count) {
            console.log(res)
        }
    }
    );

    table.on('tool(currentTableFilter)', function (obj) {

        let data = obj.data;
        if (obj.event === 'edit') {
            let index = layer.open({
                title: '订单详情',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['80%', '50%'],
                content: '/admin/order/OrderDetail?id=' + data.id,
                end: function () {
                    /*删除session无用图片*/
                    $.ajax({
                        url: '/admin/order/clearSession',
                        type: 'post',
                        dataType: 'json',
                    });
                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        }

        if (obj.event === 'mkorder') {
            let index = layer.open({
                title: '下单',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '/admin/order/MkOrder?bizpro_id=' + data.bizpro_id,
                end: function () {
                    /*删除session无用图片*/
                    $.ajax({
                        url: '/admin/order/clearSession',
                        type: 'post',
                        dataType: 'json',
                    });
                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        }

        if (obj.event === 'delete') {

            layer.confirm('真的删除行么', function (index) {

                $.ajax({
                    url: '/admin/order/DelInfo',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        id: obj.data.id,
                    },
                    success: function(data) {
                        if (data.status) {
                            layer.msg('删除成功!', {icon: 1, time: 1000});
                            $(obj)[0].tr.remove()

                        }else{
                            layer.msg(data.msg, {
                                icon: 2,
                                time: 3000
                            });
                        }
                    },

                    error: function(re) {
                        console.log(re)
                        layer.open({
                            title: '删除失败',
                            content: '<span style="color:red">'+re.responseJSON.errorCode+':</span>'+re.responseJSON.msg
                        })
                    }
                });


                // $(obj)[0].tr.remove()
            });
        }
    });


    // 监听搜索操作
    form.on('submit(data-search-btn)', function (data) {
        submitInfo();
    });
    form.on('select(selectInfo)', function (data) {
        submitInfo();
    });

    $("input[name='keywords']").on('blur',function (re) {
        submitInfo();
    })


    function submitInfo() {
        let param = {};
        let t = $('#formInfo [name]');

        $.each(t, function () {
            param[this.name] = this.value;
        });
        //let result = JSON.stringify(d);

        //执行搜索重载
        table.reload('currentTableId', {
            page: {
                curr: 1
            }
            , where: {
                //searchParams: result
                param
            }
        }, 'data');
        return false;
    };
});
